package com.ygh.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ygh.mapper.VipUserMapper;
import com.ygh.pojo.VipUser;
import com.ygh.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.UUID;

@Service
public class VipUserServiceImpl implements VipUserService{
    @Autowired
    private VipUserMapper vipUserMapper;
    /**校验用户名密码*/
    @Override
    public String getVipUser(VipUser vipUser) {
        String md5 = DigestUtils.md5DigestAsHex(vipUser.getVipPassword().getBytes());
        vipUser.setVipPassword(md5);
        QueryWrapper<VipUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("vip_iphone", vipUser.getVipIphone()).eq("vip_password", vipUser.getVipPassword());
        VipUser user = vipUserMapper.selectOne(queryWrapper);
        if(user==null){
            return null;
        }
//        String token = UUID.randomUUID().toString().replace("-", "");
        String token = user.getVipToken();
        return token;
    }
    /**用户注册*/
    @Override
    public Integer addVipUser(VipUser vipUser) {
        /**查看用户输入的手机号(用户名是否重复)*/
        QueryWrapper<VipUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("vip_iphone", vipUser.getVipIphone());
        Integer count = vipUserMapper.selectCount(queryWrapper);
        //如果有相同的手机号 则返回1 controller返回失败
        if(count!=0){
            return 1;
        }
        //用户密码进行加密
        String MD5 = DigestUtils.md5DigestAsHex(vipUser.getVipPassword().getBytes());
        vipUser.setVipPassword(MD5).setVipScore(0);
        String token = UUID.randomUUID().toString().replace("-", "");
        vipUser.setVipToken(token);
        vipUserMapper.insert(vipUser);
        //返回给controller  0   代表成功  返回成功给前端
        return 0;
    }
    /**用户信息管理  分页查询 展示用户数据*/
    @Override
    public PageResult getvipuser(PageResult pageResult) {
        //定义分页对象
        IPage<VipUser> page=new Page<>(pageResult.getPageNum(), pageResult.getPageSize());
        //定义查询构造器
        QueryWrapper<VipUser> queryWrapper = new QueryWrapper<>();
        //获取模糊查询条件
        queryWrapper.like(StringUtils.hasLength(pageResult.getQuery()), "car_id", pageResult.getQuery());
        page=vipUserMapper.selectPage(page, queryWrapper);
        //获取分页查询到的数据
        long total = page.getTotal();
        List<VipUser> records = page.getRecords();
        //封装到对象中
        pageResult.setRows(records).setTotal(total);
        //返回controller
        return pageResult;

    }
    /**用户删除*/
    @Override
    public void deleteUserByCarId(String carId) {
        vipUserMapper.deleteById(carId);
    }

    @Override
    public void updateVipUser(VipUser vipUser) {
        System.out.println(vipUser.getCarId());
        QueryWrapper<VipUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("vip_iphone", vipUser.getVipIphone());
        vipUserMapper.update(vipUser,queryWrapper);
    }

}


